$(function(){

//获取渲染用户信息
getUserInfo()

//退出按钮
$('#logout').on('click',function(){
    layer.confirm('确认退出登录吗？',{icon:3,title:'提示'},function (index){
        localStorage.removeItem('token')
        location.href = '/login.html'
        layer.close(index)
    })
})


})

function getUserInfo () {
axios({
    url:'/my/userinfo',
    method:'get',
    
}).then(({data:res})=>{
    //判断
    if (res.status != 0) return layer.msg(res.message)

    
    renderAvatar(res.data)
});

}

function renderAvatar (user){
    let name = user.nickname  ||  user.username
    $('.welcome').html(`欢迎&nbsp;`+name)
    if(user.user_pic != null ){
        $('.text-icon').hide()
        $('.layui-nav-img').show().attr('src',user.user_pic)
    }else {
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.text-icon').show().html(first)
    }



}